package com.pos.widgets.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.pos.R;

/**
 * @description:通用弹出框dialog
 * @author cai
 * @time:2015年1月20日上午10:26:54
 */
public class UpdateDialog {
	private Dialog dialog;
	private View view;
	private LayoutInflater inflater;
	private TextView text_msg, text_percent;
	private ProgressBar progressBar;
	private Button button_install;

	public UpdateDialog(Context context) {
		dialog = new Dialog(context, R.style.dialog);
		inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.update_dialog_layout, null);

		text_msg = (TextView) view.findViewById(R.id.text_msg);
		text_percent = (TextView) view.findViewById(R.id.text_percent);
		progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
		button_install = (Button) view.findViewById(R.id.button_install);
		dialog.setContentView(view);
		Window dialogWindow = dialog.getWindow();
		WindowManager m = ((Activity) context).getWindowManager();
		Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
		WindowManager.LayoutParams p = dialogWindow.getAttributes();
		// p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
		p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.65
		dialogWindow.setAttributes(p);
	}

	/**
	 * showDialog
	 * 
	 * @param message
	 *            提示语
	 */
	public void showDialog(String message, View.OnClickListener listener_confirm) {
		text_msg.setText(message);
		button_install.setOnClickListener(listener_confirm);
		dialog.show();
	}

	public void showProgress(int progress, String percent) {
		progressBar.setProgress(progress);
		text_percent.setText(percent);
	}

	public void setComplete() {
		button_install.setVisibility(View.VISIBLE);
	}

	public void dismissDialog() {
		dialog.dismiss();
	}

	public boolean isShow() {
		if (dialog.isShowing()) {
			return true;
		} else {
			return false;
		}
	}

	public Dialog getDialog() {
		return dialog;
	}

	public void setCancelable(boolean is) {
		dialog.setCancelable(is);
	}

}
